<template>
	<view class="content">
		<view class="all">
			<view class="daka-title">
				{{daka.title}}
			</view>
			<view class="time">
				<view class="daka-rekedateTime">
					<p class="name">发布时间：</p>{{daka.rekedateTime}}
				</view>
				<view class="daka-expirdateTime">
					<p class="name">截至时间：</p>{{daka.expirdateTime}}
				</view>
			</view>
			<view class="daka-content">
				{{daka.content}}
			</view>
			<view class="daka-teach">
				<p class="name">发布教师：</p>{{daka.teacherName}}
			</view>
			</view>
			<view class="button">
				<view class="submit">

				</view>
				<view class="complete">

				</view>
			</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				dakaId:0,
				daka: {
					status: null,
					homeworkId: null,
					title: '',
					content: '',
					rekedateTime: '',
					expirdateTime: '',
					teacherName: '',
					imgUrl:''
				}
			}
		},
		methods: {
			goPage(id) {
				console.log(id);
				if (id == 0) {
					uni.switchTab({
						url: '/pages/login/login'
					})
				}
			},
			onLoad(options) {
				this.dakaId = decodeURIComponent(options.id);
				this.showZuoyeDeta();
			},
			onPullDownRefresh() {
				console.log('刷新页面');
				setTimeout(() => {
					uni.stopPullDownRefresh();
					this.showZuoyeDeta();
				}, 1000)
			},
			getJwtToken() {
				try {
					return wx.getStorageSync('jwt_token');
				} catch (e) {
					// 处理获取令牌失败的逻辑，比如返回 null 或抛出错误  
					return null;
				}
			},
			// 假设你使用 uni.request 发送请求  
			sendRequestWithToken(url, method, data) {
				const token = this.getJwtToken();
				if (token) {
					wx.request({
						url: url,
						method: method,
						data:data,
						header: {
							'token': token // 将令牌添加到 Authorization 头部  
						},
						success: (res) => {
							if (res.data.code === 1 && res.data.msg === 'success') {
								let zuoye = res.data.data;
								zuoye.rekedateTime = zuoye.rekedateTime.replace('T', ' ');
								zuoye.expirdateTime = zuoye.expirdateTime.replace('T', ' ');
								this.daka = zuoye;
								
							} else if (res.data.code === 0) {
								uni.showToast({
									title: '登录信息有误',
									icon: null
								});
								this.goPage(0);
								console.error('未登录', res.data.data);
							}
						},
						fail: (error) => {
							uni.showToast({
								title: '网络请求失败',
								icon: 'none'
							});
							console.error('请求失败', error);
						}
					});
				} else {
					this.goPage(0);
					// 如果没有令牌，处理没有令牌的情况，比如跳转到登录页  
					console.error('No token found, please login first.');
				}
			},
			getuserName() {
				try {
					return wx.getStorageSync('user_name');
				} catch (e) {
					// 处理获取令牌失败的逻辑，比如返回 null 或抛出错误  
					return null;
				}
			},
			// 在某个页面的事件处理函数中  
			showZuoyeDeta() {
				
					const url = `http://localhost:8080/teacher/dakadeta`;
					const method = 'PUT';
					let data = {
						username:this.getuserName(),
						id:this.dakaId
					};
					this.sendRequestWithToken(url, method, data);
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		justify-content: center;
		flex-direction: column;
		padding-top: 100rpx;
	}
	.all{
		background-color: rgba(221,221,221, 0.1);
		border-radius: 30rpx;
	}
	.daka-title {
		display: flex;
		width:100%;
		height: 80rpx;
		font-size: 50rpx;
		font-weight: bold;
		font-family: AliMamaShuHei;
	    padding-left: 20rpx;
	}
.time{
	display: flex;
	flex-direction: column;
	width: 100%;
}
	.daka-rekedateTime {
		display: flex;
		width: 100%;
		height: 30rpx;
		font-size: 20rpx;
		  padding-left: 30rpx;
		font-family: AliMamaShuHei;
	}

	.daka-expirdateTime {
		display: flex;
		width: 100%;
		height: 30rpx;
		font-size: 20rpx;
		  padding-left: 30rpx;
		font-family: AliMamaShuHei;
		margin-bottom: 30rpx;
	}

	.name {
		width: 15%;
	}

	.daka-content {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 100%;
		height: 300rpx;
		font-size: 28rpx;
		font-family: AliMamaShuHei;
		background-color: rgba(208,208,208, 0.3); 
	}

	.daka-teach {
		display: flex;
		width: 100%;
		height: 50rpx;
		font-size: 20rpx;
		font-family: AliMamaShuHei;
		margin-top: 30rpx;
		padding-left: 70%;
	}

</style>